#!/usr/bin/env ruby
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
# frozen_string_literal: true

require 'set'
require_relative '../defconfig.rb'

start_pod

names = Set.new %w[
  ES_USER
  ES_PASSWORD
  GIT_PASSWORD
]

service_authentication = relevant_service_authentication(names)
env = docker_env(service_authentication)

DOCKER_CCI = '/c/compass-ci'
DOCKER_REPO = '/c/git-repos'
docker_rm 'git-mirror'

lkp = %x(id -u lkp)
group_id = %x(cat /etc/group | grep '^committer' | awk -F ':' '{print $3}')

cmd = %w[
  docker run
  --restart=always
  --name git-mirror
] + env + %W[
  -u #{lkp.to_i}:#{group_id.to_i}
  -d
  -e CCI_SRC=#{DOCKER_CCI}
  -e REPO_SRC=#{DOCKER_REPO}
  -e GIT_ASKPASS=/usr/local/bin/git-askpass.sh
  -v #{ENV['CCI_SRC']}:#{DOCKER_CCI}
  -v /etc/localtime:/etc/localtime:ro
  -v /etc/compass-ci/defaults/upstream-config:/etc/compass-ci/defaults/upstream-config
  -v #{ENV['REPO_SRC']}:#{DOCKER_REPO}
  -v /srv/git:/srv/git
  -w /c/compass-ci/sbin
  alpine38:git-mirror
]
cmd += ['sh', '-c', 'umask 002 && ./git-mirror.rb']
system(*cmd)
